﻿using System;
using System.Data.Entity;
using WebSiteFool.Models;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;

namespace WebSiteFool.DAL
{
    public class UnitOfWork : IDisposable
    {
        [Import]
        private DbContext _context;
        private Repository<Article> _articleRepository;
        private Repository<Picture> _pictureRepository;
        private Repository<Comment> _commentRepository;
        private Repository<VisitRecord> _visitRecordRepository;
        private Repository<UserProfile> _userRepository;

        private bool _disposed;
        private CompositionContainer _container;

        private static UnitOfWork _instance = new UnitOfWork();

        public static UnitOfWork Instance
        {
            get
            {
                return _instance;
            }
        }

        public Repository<Article> ArticleRepository
        {
            get
            {
                if (_articleRepository == null)
                {
                    _articleRepository = new Repository<Article>(_context);
                }
                return _articleRepository;
            }
        }

        public Repository<Picture> PictureRepository
        {
            get
            {
                if (_pictureRepository == null)
                {
                    _pictureRepository = new Repository<Picture>(_context);
                }
                return _pictureRepository;
            }
        }

        public Repository<Comment> CommentRepository
        {
            get
            {
                if (_commentRepository == null)
                {
                    _commentRepository = new Repository<Comment>(_context);
                }
                return _commentRepository;
            }
        }

        public Repository<VisitRecord> VisitRecordRepository
        {
            get
            {
                if (_visitRecordRepository == null)
                {
                    _visitRecordRepository = new Repository<VisitRecord>(_context);
                }
                return _visitRecordRepository;
            }
        }

        public Repository<UserProfile> UserRespository
        {
            get
            {
                if (_userRepository == null)
                {
                    _userRepository = new Repository<UserProfile>(_context);
                }
                return _userRepository;
            }
        }
        public UnitOfWork()
        {
            var catalog = new AggregateCatalog(new AssemblyCatalog(typeof(FoolDataContext).Assembly));
            _container = new CompositionContainer(catalog);
            var unitOfWork = UnitOfWork.Instance;
            _container.SatisfyImportsOnce(this);
        }

        public void Save()
        {
            _context.SaveChanges();
        }



        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool isDisposing)
        {
            if (isDisposing)
            {
                if (!_disposed)
                {
                    _context.Dispose();
                }
            }
            _disposed = true;
        }

        ~UnitOfWork()
        {
            Dispose(false);
        }
    }
}